﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion

namespace TheSpirit
{
    class FireBall : Tower
    {
        #region Initialize

        public FireBall()
        {
            attack = 10;
            delay = 5;
            range = 500;
            attackVelocity = 500;
            attackTime = 0;

            isAttack = false;

            towerAttribute = new Attribute();
            towerAttribute.attributeType = Attribute.AttributeType.Fire;

            towerAnimation = new Animation();
            attackAnimation = new Animation();

            attackPoint = Vector2.Zero;
            position = Vector2.Zero;
        }

        public override void LoadContent(ContentManager content)
        {
            towerTexture = content.Load<Texture2D>("Tower/Fire/FireBallTower");
            attackTexture = content.Load<Texture2D>("Tower/Fire/FireBall");

            towerAnimation.AddAnimation("Normal", new AnimationStrip(content.Load<Texture2D>("Tower/Fire/FireBallTowerAttack"), 100, "Normal"));
            towerAnimation.animations["Normal"].Setting(false, 1.2f);
            attackAnimation.AddAnimation("Normal", new AnimationStrip(content.Load<Texture2D>("Tower/Fire/FireBallAttack"), 50, "Normal"));
            attackAnimation.animations["Normal"].Setting(false, 0.05f);

            towerAnimation.PlayAnimation("Normal");
            //attackAnimation.PlayAnimation("Normal");
        }

        #endregion


        #region Update & Draw
        //필요하다면 Tower Class 로 이동
        public override void Update(GameTime gameTime, List<Enemy> enemy, int worldX, int worldY)
        {
            //공격 타겟 설정
            if (!isAttack)
            {
                if (attackTime + delay < gameTime.TotalGameTime.TotalSeconds)
                {
                    foreach (Enemy e in enemy)
                    {
                        destination = new Vector2(e.WorldLocation.X + 50, e.WorldLocation.Y + 50);
                        position = new Vector2(WorldLocation.X + towerTexture.Width / 2 - 25, WorldLocation.Y - 25);
                        float length = (destination - position).Length();
                        attackPoint = new Vector2(position.X - worldX, position.Y - worldY);

                        if (length < range)
                        {
                            target = e;

                            Vector2 toDestination = destination - position;
                            Vector2 velocityUnitVector = toDestination;

                            distance = toDestination.Length();
                            velocityUnitVector.Normalize();
                            velocity = velocityUnitVector * attackVelocity;

                            isAttack = true;
                            attackTime = gameTime.TotalGameTime.TotalSeconds;

                            //position = Vector2.Zero;
                            break;
                        }
                        else
                        {
                            destination = Vector2.Zero;
                            position = Vector2.Zero;
                        }
                    }
                }
                towerAnimation.updateAnimation(gameTime);
            }
            else
            {
                destination = new Vector2(target.WorldLocation.X + 50, target.WorldLocation.Y + 20);

                Vector2 toDestination = destination - position;
                Vector2 velocityUnitVector = toDestination;

                distance = toDestination.Length();
                velocityUnitVector.Normalize();
                velocity = velocityUnitVector * attackVelocity;

                float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

                Vector2 movement = velocity * elapsedSeconds;
                position += movement;
                distance -= movement.Length();

                attackPoint = new Vector2(position.X - worldX, position.Y - worldY);

                if (distance <= 0)
                {
                    velocity.Normalize();
                    position += velocity * distance;

                    velocity = Vector2.Zero;
                    //position = Vector2.Zero;
                    isAttack = false;

                    target.getDamage(attack);
                    target = null;

                    attackAnimation.PlayAnimation("Normal");
                    towerAnimation.PlayAnimation("Normal");
                }
            }

            if (attackAnimation.CurrentAnimation != null &&
                !attackAnimation.animations["Normal"].FinishedPlaying)
            {
                attackPoint = new Vector2(position.X - worldX, position.Y - worldY);
                attackAnimation.updateAnimation(gameTime);
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            towerAnimation.DrawAnimation(spriteBatch, localLocation);
            //spriteBatch.Draw(towerTexture, localLocation, Color.White);
            if (isAttack)
                spriteBatch.Draw(attackTexture, new Rectangle((int)attackPoint.X, (int)attackPoint.Y, attackTexture.Width, attackTexture.Height), Color.White);
            else if(attackAnimation.CurrentAnimation != null &&
               !attackAnimation.animations["Normal"].FinishedPlaying)
                attackAnimation.DrawAnimation(spriteBatch, new Rectangle((int)attackPoint.X, (int)attackPoint.Y, attackTexture.Width, attackTexture.Height));
        }

        #endregion
    }
}
